package cn.itcast.config;

import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author: wangxg
 * @date: 2019-06-22 05:01
 **/

@Configuration
@EnableSwagger2
public class Swagger2Config {

    /**
     * 前台api接口文档
     * @return
     */
    @Bean
    public Docket webApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                /**
                 * 过滤掉admin接口
                 */
                .paths(Predicates.not(PathSelectors.regex("/admin.*")))
                /**
                 * 过滤掉错误接口
                 */
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

    @Bean
    public Docket adminApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                /**
                 * 只显示admin接口
                 */
                .paths(Predicates.and(PathSelectors.regex("/admin/.*")))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder().title("用户中心微服务前台网站API")
                .description("此文档描述了用户中心前台网站的基本API接口")
                .version("1.0")
                .contact(new Contact("wangxg628", "https://gitee.com/wxg826/java-practice-code", "wang_xingang@163.com"))
                .build();
    }

    private ApiInfo adminApiInfo() {
        return new ApiInfoBuilder().title("用户中心微服务后台管理系统API")
                .description("此文档描述了用户中心后台管理系统的基本API接口")
                .version("1.0")
                .contact(new Contact("wangxg628", "https://gitee.com/wxg826/java-practice-code", "wang_xingang@163.com"))
                .build();
    }
}
